// 检查当前页面
if (document.getElementById('loginForm')) {
    // 登录页面逻辑
    const loginForm = document.getElementById('loginForm');
    const usernameError = document.getElementById('usernameError');
    const passwordError = document.getElementById('passwordError');
    const loginMessage = document.getElementById('loginMessage');

    // 模拟用户数据库
    const users = [
        { username: 'admin', password: '123' },
        { username: 'user1', password: '123' }
    ];

    loginForm.addEventListener('submit', async function (e) {
        e.preventDefault();

        // 获取表单值
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value;

        // 重置错误消息
        usernameError.textContent = '';
        passwordError.textContent = '';
        loginMessage.textContent = '';
        loginMessage.className = 'message';

        // 简单验证
        let isValid = true;

        if (username === '') {
            usernameError.textContent = '请输入用户名';
            isValid = false;
        }

        if (password === '') {
            passwordError.textContent = '请输入密码';
            isValid = false;
        }
        // else if (password.length < 6) {
        //     passwordError.textContent = '密码至少6位';
        //     isValid = false;
        // }

        if (!isValid) return;

        // 验证用户
        // const user = users.find(u => u.username === username && u.password === password);
        //http://localhost/api/auth/login?username=user3&password=123
        const user = await fetch(`http://localhost/api/auth/login?username=${username}&password=${password}`)
            .then(response => response.json())
            .then(
                data => data
            );


        if (user) {
            // 登录成功
            loginMessage.textContent = '登录成功，正在跳转...';
            loginMessage.className = 'message success';

            // 存储登录状态到sessionStorage
            // sessionStorage.setItem('isLoggedIn', 'true');
            // sessionStorage.setItem('username', username);

            // 2秒后跳转到主页
            setTimeout(() => {
                window.location.href = 'index.html';
            }, 1000);
        } else {
            // 登录失败
            loginMessage.textContent = '用户名或密码错误';
            loginMessage.className = 'message error';
        }
    });
} else if (document.getElementById('logoutBtn')) {
    // 主页逻辑
    const loggedInUser = document.getElementById('loggedInUser');
    const logoutBtn = document.getElementById('logoutBtn');

    // 检查登录状态
    const isLoggedIn = sessionStorage.getItem('isLoggedIn');
    const username = sessionStorage.getItem('username');

    if (isLoggedIn && username) {
        loggedInUser.textContent = username;
    } else {
        // 未登录直接访问主页，跳回登录页
        window.location.href = 'index.html';
    }

    // 退出登录
    logoutBtn.addEventListener('click', function () {
        sessionStorage.removeItem('isLoggedIn');
        sessionStorage.removeItem('username');
        window.location.href = 'index.html';
    });
}